package com.internship.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;





import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.EnterpriseInfo;
import com.internship.model.EnterpriseUser;
import com.internship.page.Page;
import com.internship.service.EnterpriseInfoService;
import com.internship.service.EnterpriseUserService;
import com.internship.util.DesTool;


@Controller
@RequestMapping("enterpriseuser")
public class EnterpriseUserController {
	private final Logger logger = Logger.getLogger(EnterpriseUserController.class);
	
	@Autowired
	private EnterpriseInfoService 	enterpriseinfoService;
	@Autowired
	private EnterpriseUserService 	enterpriseUserService;
	
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/enterpriseuser_list.do")
	public ModelAndView getEnterpriseuserList() {
		return enterpriseuser(0,null,1);
	}
	@RequestMapping("/enterpriseuser_page_list.do")
	public ModelAndView enterpriseuser(@RequestParam int EnterpriseId,@RequestParam String UserName,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("enterprise_user_list");
				Page<EnterpriseUser> pageList = enterpriseUserService.getEnterpriseuserList(EnterpriseId, UserName, pageNumber);
				List<EnterpriseInfo>  enterpriseinfoList = enterpriseinfoService.getEnterpriseList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("enterpriseinfoList",enterpriseinfoList);
				mv.addObject("UserName",UserName);
				mv.addObject("EnterpriseId",EnterpriseId);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("enterpriseuserList", pageList.getResultList());
		return mv;
	}
	/**
	 * 检验账号 是否存在
	 * check_user
	 * @return
	 */
	@RequestMapping("/enterpriseuser_check.do")
	@ResponseBody
    public String checkEnterpriseuser(@RequestParam("UserAccount") String UserAccount) {  
		try {
			UserAccount = new String(UserAccount.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		EnterpriseUser user = enterpriseUserService.getEnterpriseuser(UserAccount);
		return null==user ? "0" : "1";
	}
	/**?
	 * 添加实习单位教师信息
	 * @return
	 */
	@RequestMapping("/add_enterpriseuser.do")
	public ModelAndView addEnterpriseuser() {
		ModelAndView mv = new ModelAndView("enterprise_user_list_add");
		List<EnterpriseInfo>  enterpriseinfoList = enterpriseinfoService.getEnterpriseList();
		mv.addObject("enterpriseinfoList",enterpriseinfoList);
		return mv;
	}
	@RequestMapping("/enterpriseuser_save.do")
	@ResponseBody
	public JSONObject saveEenterpriseuser(@RequestParam("UserAccount")String  UserAccount,@RequestParam("UserName") String UserName,@RequestParam("EnterpriseId") int EnterpriseId,@RequestParam("UserPassword") String UserPassword,@RequestParam("UserSex") String UserSex,@RequestParam("UserDepartment") String UserDepartment,@RequestParam("UserJobTitle") String UserJobTitle,@RequestParam("UserPhone") String UserPhone,@RequestParam("UserEmail") String UserEmail) {
		JSONObject json = new JSONObject();
		EnterpriseUser enterpriseUser = new EnterpriseUser();
		enterpriseUser.setUserAccount(UserAccount);
		enterpriseUser.setEnterpriseId(EnterpriseId);
		enterpriseUser.setUserDepartment(UserDepartment);
		enterpriseUser.setUserEmail(UserEmail);
		enterpriseUser.setUserPhone(UserPhone);
		enterpriseUser.setUserJobTitle(UserJobTitle);
		enterpriseUser.setUserName(UserName);
		enterpriseUser.setUserPhone(UserPhone);
		enterpriseUser.setUserSex(UserSex);
		enterpriseUser.setUserPassword(DesTool.encode(UserPassword));
		int  rel = enterpriseUserService.addEnterpriseuser(enterpriseUser);
		json.put("result", rel>0?"success":"error");
		return json;
	}
	/**?
	 * 编辑实习单位教师信息
	 * @return
	 */
	@RequestMapping("/edit_enterpriseuser.do")
	public ModelAndView editEnterpriseuser(@RequestParam String UserAccount) {
		ModelAndView mv = new ModelAndView("enterprise_user_list_edit");
		EnterpriseUser enterpriseUser  =enterpriseUserService.getEnterpriseuser(UserAccount);
		System.out.println(enterpriseUser.getName());
		mv.addObject("enterpriseUser",enterpriseUser);
		return mv;
	}
	@RequestMapping("/enterpriseuser_update.do")
	@ResponseBody
	public JSONObject updateEenterpriseuser(@RequestParam("UserAccount")String  UserAccount,@RequestParam("UserName") String UserName,@RequestParam("UserPassword") String UserPassword,@RequestParam("UserSex") String UserSex,@RequestParam("UserDepartment") String UserDepartment,@RequestParam("UserJobTitle") String UserJobTitle,@RequestParam("UserPhone") String UserPhone,@RequestParam("UserEmail") String UserEmail) {
		JSONObject json = new JSONObject();
		EnterpriseUser enterpriseUser = enterpriseUserService.getEnterpriseuser(UserAccount);
		enterpriseUser.setUserAccount(UserAccount);
		enterpriseUser.setUserDepartment(UserDepartment);
		enterpriseUser.setUserEmail(UserEmail);
		enterpriseUser.setUserPhone(UserPhone);
		enterpriseUser.setUserJobTitle(UserJobTitle);
		enterpriseUser.setUserName(UserName);
		enterpriseUser.setUserPhone(UserPhone);
		enterpriseUser.setUserSex(UserSex);
		if (!enterpriseUser.getUserPassword().equals(UserPassword)) {
			enterpriseUser.setUserPassword(DesTool.encode(UserPassword));
		}
		int  rel = enterpriseUserService.updateEnterpriseinfo(enterpriseUser);
		json.put("result", rel>0?"success":"error");
		return json;
	}
	/**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delEnterpriseuser(@RequestParam String UserAccount){
			JSONObject json = new JSONObject();
			try {
				UserAccount = new String(UserAccount.getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			int  rel= enterpriseUserService.delEnterpriseinfo(UserAccount);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
}